package com.dayuanit.chat;

import java.io.*;
import java.net.Socket;
import java.util.Properties;

public class Client {
    private static String serverIP = "192.168.5.112";
    private static int sreverPort = 9999;

    private static Socket socket;

    static {

    }

    //读取键盘输入的信息
    public static void readKeyBoard() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String msg = null;
        //读取键盘输入的信息
        while (null != (msg = br.readLine())) {
            bufferedWriter.write(msg);
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
    }

    //接收服务器端发送的消息
    public static void receiveMsgFromServer() throws Exception {
        System.out.println("------------------接收服务器的消息...---------------------");
        //客户端接收服务器端发过来的消息
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String msg = null;
        while (null != (msg = bufferedReader.readLine())) {
            System.out.println("接收到服务器的消息:->" + msg);
        }
    }

    //通过命令行参数的方式，灵活，但是对于运维人员不友好，不够直观，如意出错。
    private static void initSocket(String[] args) {
        try {
            if (args.length < 2) {
                socket = new Socket(serverIP, sreverPort);
                return;
            }
            socket = new Socket(args[0], Integer.parseInt(args[1]));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //使用File流，要使用绝对路径，不利于跨平台，差评。
    private static void initSocket2(String propertiesPath) throws Exception {
        FileInputStream fis = new FileInputStream(propertiesPath);
        Properties properties = new Properties();
        properties.load(fis);//加载配置文件
        String ip = properties.getProperty("server.ip");
        String port = properties.getProperty("server.port");

        socket = new Socket(ip, Integer.parseInt(port));
    }

    //使用类加载器 加载资源文件 可以使用相对路径 代码扩展性好 推荐
    private static void initSocket3(String propertiesPath) throws Exception {

        //获取Client类的字节码对象
        Class<Client> claz = Client.class;

        //通过字节码对象获取类加载器对象
        ClassLoader classLoader = claz.getClassLoader();

        //通过类加载器，加载classpath下面的资源文件
        InputStream resourceAsStream = classLoader.getResourceAsStream(propertiesPath);

        Properties properties = new Properties();
        properties.load(resourceAsStream);//加载配置文件
        String ip = properties.getProperty("server.ip");
        String port = properties.getProperty("server.port");

        System.out.println("ip=" + ip + " port=" + port);
        socket = new Socket(ip, Integer.parseInt(port));
    }

    public static void main(String[] args) throws Exception {
        String xxx = "server.ip=212.64.69.194";

//        initSocket(args);

        // ！！！！路径千万不能写成绝对路径，因为别人未必有D盘，未必有次目录。  谁写谁回家！！！！！
//        initSocket2("D:\\dayuanit\\15\\javase-idea\\wechat\\src\\com\\dayuanit\\chat\\wechat.properties");

        //使用相对路径加载资源 推荐五颗星
        initSocket3("chat/wechat.properties");

        //客户端 也就是聊天的用户

        //由于客户端既要读取键盘的输入，又要读取服务器端的信息，两者是并行的，不是串行的，所以，要开启两个线程
        //来负责执行这两个方法。
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    readKeyBoard();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "readKeyBoardThread").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    receiveMsgFromServer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"receiveServerMsgThread").start();

    }
}
